﻿//-----------------------------------------------------------------------
// <copyright file="GroupFactory.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;

    internal sealed class GroupFactory
    {
        private readonly Dictionary<MessengerEntity, Group> groups = new Dictionary<MessengerEntity, Group>();

        private readonly User user;

        public GroupFactory(User user)
        {
            this.user = user;
        }

        public Group FindOrCreate(object obj)
        {
            if (obj == null)
            {
                return null;
            }

            return FindOrCreate(new MessengerEntity(obj));
        }

        public Group FindOrCreate(MessengerEntity entity)
        {
            Group group = null;

            if (this.groups.TryGetValue(entity, out group))
            {
                return group;
            }

            group = new Group(entity, this.user);

            this.groups.Add(entity, group);

            return group;
        }
    }
}